package com.smec.remes.util;

import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.smec.moblie.SMECApplication;

import java.util.List;

/**
 * Created by xupeizuo on 2017/7/3.
 */

public class CommonUtils {

    /**
     * 判断List是否为空
     */
    public static boolean notEmpty(List<?> T){
        return T!=null && !T.isEmpty();
    }

    /**
     * 隐藏键盘
     * @param view
     */
    public static void hideImmManager(View view) {
        InputMethodManager imm = (InputMethodManager) SMECApplication.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * 弹出键盘
     * @param view
     */
    public static void showImmManager(View view) {
        InputMethodManager imm = (InputMethodManager) SMECApplication.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, 0);
    }

    /**
     * 判断当前程序运行在前台还是后台
     * @param context
     * @return
     */
    public static boolean isForeground(Context context)
    {
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {

                if (appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    Log.i(context.getPackageName(), "处于后台"
                            + appProcess.processName);
                    return false;
                } else {
                    Log.i(context.getPackageName(), "处于前台"
                            + appProcess.processName);
                    return true;
                }
            }
        }
        return true;
    }

}
